<?php
/**
 * 用户列表
 * User: zhangyale
 * Date: 2023/10/20
 */

namespace App\Service\Core\User\User;

use App\Component\ListService\ListAbstract;
use App\Model\Core\Company\CompanyModel;
use App\Model\Core\User\UserModel;
use App\Service\ParamsService;

/**
 * @method $this setUserIds($val)
 * @method $this setMobile($val)
 * @method $this setStatus($val)
 * @method $this setCompanyId($val)
 * @package App\Service\Core\Sys\User
 */
class UserListService extends ListAbstract
{
    //允许获取的字段
    protected array $allow_fields = ['*'];
    //允许获取的扩展字段
    protected array $allow_ext_fields = ['status_name', 'company_name', 'sex_name'];
    //默认返回的字段
    protected array $default_fields = [];
    protected array $allow_sort = ['login_at', 'created_at', 'user_id'];
    protected array $default_sort_by = ['user_id' => 'desc'];

    public function setUsername($val)
    {
        $this->params['username'] = trim($val);
        return $this;
    }

    protected function getQueryObj()
    {
        // TODO: Implement getQueryObj() method.
        $UserModel = UserModel::getInstance();
        $query_obj = $UserModel->getConnectionTable();
        //用户名称
        $username = $this->getParam('username');
        if (!empty($username)) {
            $query_obj->where('username', 'like', $username);
        }
        //用户ID
        $user_ids = $this->getParam('user_ids');
        $user_ids = easy_int_explode($user_ids);
        if (!empty($user_ids)) {
            $query_obj->whereIn('user_id', $user_ids);
        }
        //绑定手机号
        $mobile = $this->getParam('mobile');
        if (!empty($mobile)) {
            $query_obj->where('mobile', $mobile);
        }
        //单位
        $company_id = $this->getParam('company_id');
        if (is_array($company_id) && !empty($company_id)) {
            $query_obj->whereIn('company_id', $company_id);
        } else {
            if (is_numeric($company_id) && $company_id > 0) {
                $query_obj->where('company_id', $company_id);
            }
        }
        //状态
        $status = $this->getParam('status');
        if (is_array($status) && !empty($status)) {
            $query_obj->whereIn('status', $status);
        } else {
            if (is_numeric($status)) {
                $query_obj->where('status', $status);
            }
        }
        //开始时间
        $created_at_begin = $this->getParam('created_at_begin');
        if (!empty($created_at_begin)) {
            $created_at_begin = date('Y-m-d H:i:s', strtotime($created_at_begin));
            $query_obj->where('created_at', '>=', $created_at_begin);
        }
        //结束时间
        $created_at_end = $this->getParam('created_at_end');
        if (!empty($created_at_end)) {
            $created_at_end = date('Y-m-d H:i:s', strtotime($created_at_end));
            $query_obj->where('created_at', '<=', $created_at_end);
        }
        return $query_obj;
    }

    /**
     * 获取扩展字段
     * @param array $row
     * @return array
     */
    protected function getExtField(array $row)
    {
        if (easy_in_array('status_name', $this->ext_fields)) {
            $row['status_name'] = ParamsService::$status[$row['status']];
        }
        if (easy_in_array('company_name', $this->ext_fields)) {
            $company_info = CompanyModel::getInstance()->getOne($row['company_id']);
            $row['company_name'] = $company_info['company_name'] ?? '';
        }
        if (easy_in_array('sex_name', $this->ext_fields)) {
            $row['sex_name'] = ParamsService::$sex[$row['sex']];
        }
        return $row;
    }
}
